package com.sxj.springsourcecode.springframework.factory.support;

import com.sxj.springsourcecode.springframework.factory.BeanFactory;
import com.sxj.springsourcecode.springframework.factory.config.BeanDefinition;
import lombok.extern.slf4j.Slf4j;

/**
 * @ProjectName: spring-source-code
 * @Package: com.sxj.springsourcecode.springframework.factory.support
 * @ClassName: AbstractBeanFactory
 * @Author: sxj
 * @Description:
 * @Date: 2024/2/21 11:32
 * @Version: 1.0.0
 */
@Slf4j
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegestry implements BeanFactory {
    @Override
    public Object getBean(String beanName) {
        return getBean(beanName,null);
    }

    @Override
    public Object getBean(String beanName, Object... args) {
        //1 从缓存中获取
        Object bean = getSingleton(beanName);
        if (bean != null){
            return bean;
        }

        //2 没有，则创建bean
        //2.1 获取bean定义
        BeanDefinition beanDefinition = getBeanDefinition(beanName);

        //2.2 创建bean
        bean = createBean(beanName, beanDefinition,args);
        return bean;
    }

    /**
     * 获取bean定义
     * @param beanName
     */
    public abstract BeanDefinition getBeanDefinition(String beanName);

    /**
     * 创建bean
     * @param beanName
     * @param beanDefinition
     */
    public abstract Object createBean(String beanName, BeanDefinition beanDefinition,Object[] args);
}
